package com.easy.auth.controller;

import com.easy.common.bean.SysUser;
import com.easy.common.exception.ParameterException;
import com.easy.common.service.IUserService;
import com.easy.common.util.JWTUtil;
import com.easy.common.util.PasswordDecode;
import com.easy.common.util.ResultData;
import com.easy.common.util.SYSUtil;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

@RestController
@RequestMapping("auth")
@CrossOrigin


public class AuthController {
        @DubboReference
        IUserService userService;
        @PostMapping("login")
        public ResultData login(@RequestBody SysUser user){
            //获取用户名和密码
            String username=user.getUsername();
            String password=user.getPassword();
            //验证参数
            if(SYSUtil.checkNotEmpty(username,password)){
                //通过用户名获取用户数据
                //需要访问user模块中的服务
                SysUser sysUser=userService.getByUsername(username);
                if(SYSUtil.checkNull(sysUser)){
                    //比较密码
                    String password_data=sysUser.getPassword();
                    if(PasswordDecode.decode(password,password_data)){
                        //如果成功签发token
                        String token=JWTUtil.generateToken(sysUser.getUsername(), Arrays.asList("user::insert","user::delete"));
                        return ResultData.success(token);
                    }else{
                        throw new RuntimeException("密码不正确");
                    }
                }else{
                    throw new ParameterException("没有此账号");
                }
            }else{
                throw new ParameterException("参数异常");
            }
            //验证密码是否正确
            //返回对应结果
        }
}

